from examination.pojo.test_paper import TestPaper
from examination.pojo.topic import Topic
from examination.scoring.factory.scoring_factory import ScoringFactory


class Examiner:
    def __init__(self):
        pass

    def scoring(self, test_paper: TestPaper, student_test_paper: TestPaper):
        score = 0
        correct_paper:TestPaper = test_paper.topic_list
        student_paper:TestPaper = student_test_paper.topic_list

        for num, correct_topic in enumerate(correct_paper):
            student_topic = student_paper[num]
            topic_type: str = correct_topic.topic_type
            student_score = ScoringFactory.getScoringUtils(topic_type).get_score(
                correct_topic.topic_answer, student_topic.topic_answer, correct_topic.topic_score
            )
            student_topic.topic_score = student_score
            score = score + student_score
        return score
    
    def test(self):
        testPaper = TestPaper()
        # 创建一些测试数据
        testPaper.topic_list = [
            Topic(
                topic_id=1,
                topic_type="code",
                topic_num=1,
                topic_context="请实现一个函数，输入一个字符串，输出该字符串反转后的字符串。",
                topic_answer="def reverse_string(s):\n    return s[::-1]",
                topic_score=100,
            ),
        ]
        # 创建一个试卷
        test_paper = TestPaper()
        # 创建一些测试数据
        test_paper.topic_list = [
            Topic(
                topic_id=1,
                topic_type="code",
                topic_num=1,
                topic_context="请实现一个函数，输入一个字符串，输出该字符串反转后的字符串。",
                topic_answer="def reverse_string(s):\n    return s[::-1]",
                topic_score=100,
            ),
        ]
        examiner = Examiner()
        return examiner.scoring(test_paper, testPaper)
 
if __name__ == "__main__":
    testPaper = TestPaper()
    # 创建一些测试数据
    testPaper.topic_list = [
        Topic(
            topic_id=1,
            topic_type="code",
            topic_num=1,
            topic_context="请实现一个函数，输入一个字符串，输出该字符串反转后的字符串。",
            topic_answer="def reverse_string(s):\n    return s[::-1]",
            topic_score=100,
        ),
    ]
    # 创建一个试卷
    test_paper = TestPaper()
    # 创建一些测试数据
    test_paper.topic_list = [
        Topic(
            topic_id=1,
            topic_type="code",
            topic_num=1,
            topic_context="请实现一个函数，输入一个字符串，输出该字符串反转后的字符串。",
            topic_answer="def reverse_string(s):\n    return s[::-1]",
            topic_score=100,
        ),
    ]
    examiner = Examiner()
    examiner.scoring(test_paper, testPaper)
